package com.rchat.pocmini.activity.chat;

import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;

import com.airtalkee.sdk.OnMediaAudioVisualizerListener;
import com.airtalkee.sdk.entity.AirContact;
import com.airtalkee.sdk.entity.AirSession;
import com.aso.common.utils.ToastUtils;
import com.rchat.pocmini.activity.chatmessage.ChatMessageMvpActivity;
import com.rchat.pocmini.activity.fragment.groups.GroupsPresenterImpl;
import com.rchat.pocmini.activity.groupmember.GroupMemberMvpActivity;
import com.rchat.pocmini.activity.invitefriendtogroup.InviteFriendToGroupMvpActivity;
import com.rchat.pocmini.activity.listener.MediaListener;
import com.rchat.pocmini.activity.location.LocationMvpActivity;
import com.rchat.pocmini.bluetooth.BTService;
import com.rchat.pocmini.media.PocMediaVisualizer;
import com.rchat.pocmini.service.PocService;
import com.rchat.pocmini.session.PocAirtalkeeSessionManager;
import com.rchat.pocmini.util.Sound;

import java.util.ArrayList;

/**
 * Created by ljd on 2017/8/31.
 */

public class ChatPresenterImpl extends ChatContract.Presenter implements OnMediaAudioVisualizerListener,MediaListener {

    private AirSession airSession;
    private PocAirtalkeeSessionManager sessionManager;
    private PocMediaVisualizer mediaVisualizer;
    private String groupId;

    @Override
    public void onPStart() {
        super.onPStart();
        sessionManager = PocAirtalkeeSessionManager.getPocAirtalkeeSessionManagerInstance();


        if (PocService.getInstance() != null) {
            PocService.getInstance().setMediaListener(this);
        }

        //设置频谱
        mediaVisualizer = PocMediaVisualizer.getPocMediaVisualizerInstance();
        mediaVisualizer.setMediaAudioVisualizerValid(true);
        mediaVisualizer.setMediaAudioVisualizerSpectrumNumber(1);
        mediaVisualizer.setOnMediaAudioVisualzerListener(this);
    }

    @Override
    void talkRequest() {
        if (PocService.isPhoneCalling){
            ToastUtils.showToast("当前PTT不可用");
            return;
        }
        sessionManager.talkRequest(airSession);
    }

    @Override
    void talkRelease() {
        sessionManager.talkRelease(airSession);
    }

    @Override
    void sessionCall(String groupId) {
        this.groupId = groupId;
        airSession = sessionManager.sessionCall(groupId);
        PocAirtalkeeSessionManager.getPocAirtalkeeSessionManagerInstance().setLockSessionId(groupId);
    }

    @Override
    void gotoGroupMemberActivity(String groupName,String groupId) {
        Intent groupMemberIntent = new Intent();
        groupMemberIntent.putExtra(GroupsPresenterImpl.KEY_GROUPID, groupId);
        groupMemberIntent.putExtra("groupName", groupName);
//        groupMemberIntent.setClass(mActivity, GroupMemberActivity.class);
        groupMemberIntent.setClass(mActivity, GroupMemberMvpActivity.class);
        mActivity.startActivity(groupMemberIntent);
    }

    @Override
    void gotoChatMessageActivity(String groupName, String groupId) {
        Intent chatMessageActivityIntent = new Intent();
        chatMessageActivityIntent.putExtra(GroupsPresenterImpl.KEY_GROUPID, groupId);
        chatMessageActivityIntent.putExtra("groupName", groupName);
//        chatMessageActivityIntent.setClass(mActivity, ChatMessageActivity.class);
        chatMessageActivityIntent.setClass(mActivity, ChatMessageMvpActivity.class);
        mActivity.startActivity(chatMessageActivityIntent);
    }

    @Override
    void gotoInviteFriendToGroupActivity(String groupId) {
        Intent intent = new Intent();
        intent.putExtra(GroupsPresenterImpl.KEY_GROUPID, groupId);
        intent.setClass(mActivity, InviteFriendToGroupMvpActivity.class);
        mActivity.startActivity(intent);
    }

    @Override
    void gotoLocationActivity(String groupName,String groupId) {
        Intent locationIntent;
//        locationIntent = new Intent(mActivity, LocationActivity.class);
        locationIntent = new Intent(mActivity, LocationMvpActivity.class);
        locationIntent.putExtra(GroupsPresenterImpl.KEY_GROUPID, groupId);
        locationIntent.putExtra("groupName", groupName);
        mActivity.startActivity(locationIntent);
    }

    @Override
    public void onMediaAudioVisualizerChanged(byte[] bytes, int i) {
        mView.showVoiceLevel(bytes[0]);
    }

    @Override
    public void onMediaStateTalkPreparing(AirSession var1) {
//        callSystemVibrator();
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaStateTalk(AirSession var1) {
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaStateTalkEnd(AirSession var1, int var2) {
        Sound.playSound(Sound.PLAYER_MEDIA_ME_OFF, mActivity);
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaStateListen(AirSession var1, AirContact var2) {
        if(!groupId.equals(var1.getSessionCode())){
            /**
             * Modify 李锦锻 by 2017/8/14
             * 如果不是当前频道的人在讲话就不显示谁在讲话。
             * 由于监听的频道最多有5个，但是语音回调只有一个，所以只能我们区分当前频道是不是用户进入的频道。
             */
            return;
        }
        Sound.playSound(Sound.PLAYER_MEDIA_OTHER_ON, mActivity);
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaStateListenEnd(AirSession var1) {
        Sound.playSound(Sound.PLAYER_MEDIA_OTHER_OFF, mActivity);
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaStateListenVoice(AirSession var1) {
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaQueue(AirSession var1, ArrayList<AirContact> var2) {
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaQueueIn(AirSession var1) {
        setSessionDisplay(var1);
    }

    @Override
    public void onMediaQueueOut(AirSession var1) {
        setSessionDisplay(var1);
    }


    /**
     * 根据说话状态显示UI
     *
     * @param airSession
     */
    private void setSessionDisplay(AirSession airSession) {
        mView.setSessionDisplay(airSession);
    }

    /*
     * 数组参数意义：第一个参数为等待指定时间后开始震动，震动时间为第二个参数。后边的参数依次为等待震动和震动的时间
	 * 第二个参数为重复次数，-1为不重复，0为一直震动
	 */
//    private void callSystemVibrator() {
//        Vibrator vibrator;
//        vibrator = (Vibrator) mActivity.getSystemService(Context.VIBRATOR_SERVICE);
//        vibrator.vibrate(new long[]{0, 150}, -1);
//    }

    @Override
    public void onPDestroy() {
        if (PocService.getInstance() != null) {
            PocService.getInstance().unregisterMediaListener(this);
        }
        mediaVisualizer.setOnMediaAudioVisualzerListener(null);
        super.onPDestroy();
    }
}
